home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / rtpacket.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  22KB  |  850 lines

  1. /*
  2.  
  3.           RTP input packet construction and parsing
  4.  
  5. */
  6.  
  7. #include "speakfree.h"
  8. #include <pwd.h>
  9. #include <sys/param.h>
  10.  
  11. audio_descr_t adt[] = {
  12. /* enc       sample ch */
  13.   {AE_PCMU,  8000, 1},    /*  0 PCMU */
  14.   {AE_MAX,   8000, 1},    /*  1 1016 */
  15.   {AE_G721,  8000, 1},    /*  2 G721 */
  16.   {AE_GSM,   8000, 1},    /*  3 GSM */
  17.   {AE_G723,  8000, 1},    /*  4 Unassigned */
  18.   {AE_IDVI,  8000, 1},    /*  5 DVI4 */
  19.   {AE_IDVI, 16000, 1},    /*  6 DVI4 */
  20.   {AE_LPC,   8000, 1},    /*  7 LPC */
  21.   {AE_PCMA,  8000, 1},    /*  8 PCMA */
  22.   {AE_MAX,    0, 1},    /*  9 G722 */
  23.   {AE_L16,  44100, 2},    /* 10 L16 */
  24.   {AE_L16,  44100, 1},    /* 11 L16 */
  25.   {AE_MAX,    0, 1},    /* 12 */
  26. };
  27.  
  28. #define MAX_MISORDER 100
  29. #define MAX_DROPOUT  3000
  30.  
  31. /*  ISRTP  --  Determine if a packet is RTP or not.  If so, convert
  32.            in place into a sound buffer.  */
  33.  
  34. int isrtp(pkt, len)
  35.   unsigned char *pkt;
  36.   int len;
  37. {
  38. #ifdef RationalWorld
  39.     rtp_hdr_t *rh = (rtp_hdr_t *) pkt;
  40. #endif
  41.  
  42.     unsigned int r_version, r_p, r_x, r_cc, r_m, r_pt,
  43.          r_seq, r_ts;
  44.  
  45.     /* Tear apart the header in a byte- and bit field-order
  46.        independent fashion. */
  47.  
  48.     r_version = (pkt[0] >> 6) & 3;
  49.     r_p = !!(pkt[0] & 0x20);
  50.     r_x = !!(pkt[0] & 0x10);
  51.     r_cc = pkt[0] & 0xF;
  52.     r_m = !!(pkt[1] & 0x80);
  53.     r_pt = pkt[1] & 0x1F;
  54.     r_seq = ntohs(*((short *) (pkt + 2)));
  55.     r_ts = ntohl(*((long *) (pkt + 4)));
  56.  
  57.     if (
  58. #ifdef RationalWorld
  59.     rh->version == RTP_VERSION && /* Version ID correct */
  60.     rh->pt < ELEMENTS(adt) &&     /* Payload type credible */
  61.     adt[rh->pt].sample_rate != 0 && /* Defined payload type */
  62.                       /* Padding, if present, is plausible */
  63.     (!rh->p || (pkt[len - 1] < (len - (12 + 4 * rh->cc))))
  64. #else
  65.     r_version == RTP_VERSION &&   /* Version ID correct */
  66.     r_pt < ELEMENTS(adt) &&       /* Payload type credible */
  67.     adt[r_pt].sample_rate != 0 && /* Defined payload type */
  68.                       /* Padding, if present, is plausible */
  69.     (!r_p || (pkt[len - 1] < (len - (12 + 4 * r_cc))))
  70. #endif
  71.        ) {
  72.     struct soundbuf sb;
  73.     unsigned char *payload;
  74.     int lex, paylen;
  75.  
  76.                   /* Length of fixed header extension, if any */
  77.     lex = r_x ? (ntohs(*((short *) (pkt + 2 + 12 + 4 * r_cc))) + 1) * 4 : 0;
  78.     payload = pkt + (12 + 4 * r_cc) + lex; /* Start of payload */
  79.     paylen = len - ((12 + 4 * r_cc) +      /* Length of payload */
  80.             lex + (r_p ? pkt[len - 1] : 0));
  81.     sb.compression = fProtocol;
  82.     sb.buffer.buffer_len = 0;
  83.  
  84. #ifdef NEEDED
  85.     /* Fake an RTP unique host name from the SSRC identifier. */
  86.  
  87.         sprintf(sb.sendinghost, ".RTP:%02X%02X%02X%02X",
  88.         pkt[8], pkt[9], pkt[10], pkt[11]);
  89. #else
  90.         strcpy(sb.sendinghost, ".RTP");
  91. #endif
  92.  
  93.     switch (adt[r_pt].encoding) {
  94.  
  95.         case AE_PCMU:
  96.         sb.buffer.buffer_len = paylen;
  97.         bcopy(payload, sb.buffer.buffer_val, paylen);
  98.         break;
  99.  
  100.         case AE_PCMA:
  101.                 /* Untested: I haven't found a program which sends in this
  102.            format. */
  103.         {
  104.             int i;
  105.             unsigned char *op = (unsigned char *) sb.buffer.buffer_val;
  106.  
  107.             sb.buffer.buffer_len = paylen;
  108.             for (i = 0; i < paylen; i++) {
  109.             *op++ = alaw2ulaw(*payload++);
  110.             }
  111.         }
  112.         break;
  113.  
  114.         case AE_GSM:
  115.         sb.buffer.buffer_len = paylen + sizeof(short);
  116.         bcopy(payload, sb.buffer.buffer_val + 2, paylen);
  117.         *((short *) sb.buffer.buffer_val) =
  118.             htons((short) ((((long) paylen) * 160) / 33));
  119.         sb.compression |= fCompGSM;
  120.         break;
  121.  
  122.         case AE_IDVI:
  123.         bcopy(payload + 4, sb.buffer.buffer_val, paylen - 4);
  124.         bcopy(payload, sb.buffer.buffer_val + (paylen - 4), 3);
  125.         sb.buffer.buffer_len = paylen - 1;
  126.         if (adt[r_pt].sample_rate == 8000) {
  127.             sb.compression |= fCompADPCM;
  128.         } else {
  129.  
  130.             /* Bogus attempt to convert sampling rate.    We
  131.                        really need to do this in linear mode, which isn't
  132.                supported on all SPARCs.  This is better than
  133.                nothing, though. */
  134.  
  135.             int inc = adt[r_pt].sample_rate / 8000, i;
  136.             unsigned char *in = (unsigned char *) sb.buffer.buffer_val,
  137.                   *out = (unsigned char *) sb.buffer.buffer_val;
  138.  
  139.             adpcmdecomp(&sb);
  140.             for (i = 0; i < (paylen - 4) / inc; i++) {
  141.             *out++ = *in;
  142.             in += inc;
  143.             }
  144.             sb.buffer.buffer_len /= inc;
  145.         }
  146.         break;
  147.  
  148.         case AE_LPC:
  149.         {
  150.             int i, n = paylen / 14;
  151.             char *ip = (char *) payload,
  152.              *op = (char *) sb.buffer.buffer_val + 2;
  153.  
  154. /*xd(stderr, pkt, len, TRUE);*/
  155.             *((short *) sb.buffer.buffer_val) = htons(160 * n);
  156.             for (i = 0; i < n; i++) {
  157.             bcopy(ip, op, 3);
  158.             op[3] = 0;
  159.             bcopy(ip + 3, op + 4, 10);
  160.             ip += 14;
  161.             op += 14;
  162.             }
  163.             sb.buffer.buffer_len = paylen + 2;
  164.             sb.compression |= fCompLPC;
  165. /*xd(stderr, &sb, (sizeof sb - BUFL) + sb.buffer.buffer_len, TRUE);*/
  166.         }
  167.         break;
  168.  
  169.         case AE_L16:
  170.         if (adt[r_pt].channels == 1) {
  171.             int i, j, k;
  172.         
  173.             for (i = j = k = 0; i < (paylen / 8); i++) {
  174.             if ((k & 3) != 2  && ((i % 580) != 579)) {
  175.                 sb.buffer.buffer_val[j++] =
  176.                 audio_s2u((((unsigned short *) payload)[i * 4]));
  177.             }
  178.             k = (k + 1) % 11;
  179.             }
  180.             sb.buffer.buffer_len = j;
  181.         } else if (adt[r_pt].channels == 2) {
  182.             int i, j, k;
  183.         
  184.             for (i = j = k = 0; i < (paylen / 16); i++) {
  185.             if ((k & 3) != 2  && ((i % 580) != 579)) {
  186.                 sb.buffer.buffer_val[j++] =
  187.                 audio_s2u(((((unsigned short *) payload)[i * 8]) +
  188.                        (((unsigned short *) payload)[i * 8 + 1])) / 2);
  189.             }
  190.             k = (k + 1) % 11;
  191.             }
  192.             sb.buffer.buffer_len = j;
  193.         }
  194.         break;
  195.  
  196.         default:
  197.         /* Unknown compression type. */
  198.         break;
  199.     }
  200.     bcopy(&sb, pkt, ((sizeof sb - BUFL)) + sb.buffer.buffer_len);
  201.     return TRUE;
  202.     }
  203.     return FALSE;
  204. }
  205.  
  206. /*  ISVALIDRTCPPACKET  --  Consistency check a packet to see if
  207.                is a compliant RTCP packet.    Note that
  208.                since this must also accept Speak Freely
  209.                SDES packets, the test on the protocol is
  210.                not as tight as were it exclusively for
  211.                RTP.  */
  212.  
  213. int isValidRTCPpacket(p, len)
  214.   unsigned char *p;
  215.   int len;
  216. {
  217.     unsigned char *end;
  218.  
  219.     if (((((p[0] >> 6) & 3) != RTP_VERSION) &&       /* Version incorrect ? */
  220.     ((((p[0] >> 6) & 3) != 1))) ||           /* Allow Speak Freely too */
  221.     ((p[0] & 0x20) != 0) ||            /* Padding in first packet ? */
  222.     ((p[1] != RTCP_SR) && (p[1] != RTCP_RR))) { /* First item not SR or RR ? */
  223.     return FALSE;
  224.     }
  225.     end = p + len;
  226.  
  227.     do {
  228.     /* Advance to next subpacket */
  229.     p += (ntohs(*((short *) (p + 2))) + 1) * 4;
  230.     } while (p < end && (((p[0] >> 6) & 3) == RTP_VERSION));
  231.  
  232.     return p == end;
  233. }
  234.  
  235. /*  ISRTCPBYEPACKET  --  Test if this RTCP packet contains a BYE.  */
  236.  
  237. int isRTCPByepacket(p, len)
  238.   unsigned char *p;
  239.   int len;
  240. {
  241.     unsigned char *end;
  242.     int sawbye = FALSE;
  243.                            /* Version incorrect ? */
  244.     if ((((p[0] >> 6) & 3) != RTP_VERSION && ((p[0] >> 6) & 3) != 1) ||
  245.     ((p[0] & 0x20) != 0) ||            /* Padding in first packet ? */
  246.     ((p[1] != RTCP_SR) && (p[1] != RTCP_RR))) { /* First item not SR or RR ? */
  247.     return FALSE;
  248.     }
  249.     end = p + len;
  250.  
  251.     do {
  252.     if (p[1] == RTCP_BYE) {
  253.         sawbye = TRUE;
  254.     }
  255.     /* Advance to next subpacket */
  256.     p += (ntohs(*((short *) (p + 2))) + 1) * 4;
  257.     } while (p < end && (((p[0] >> 6) & 3) == RTP_VERSION));
  258.  
  259.     return (p == end) && sawbye;
  260. }
  261.  
  262. /*  ISRTCPAPPPACKET  --  Test if this RTCP packet contains a APP item
  263.              with a given name.  If so, returns a pointer
  264.              to the APP sub-packet in app_ptr.  */
  265.  
  266. int isRTCPAPPpacket(p, len, name, app_ptr)
  267.   unsigned char *p;
  268.   int len;
  269.   char *name;
  270.   unsigned char **app_ptr;
  271. {
  272.     unsigned char *end;
  273.  
  274.     *app_ptr = NULL;
  275.                            /* Version incorrect ? */
  276.     if ((((p[0] >> 6) & 3) != RTP_VERSION && ((p[0] >> 6) & 3) != 1) ||
  277.     ((p[0] & 0x20) != 0) ||            /* Padding in first packet ? */
  278.     ((p[1] != RTCP_SR) && (p[1] != RTCP_RR))) { /* First item not SR or RR ? */
  279.     return FALSE;
  280.     }
  281.     end = p + len;
  282.  
  283.     do {
  284.     if ((p[1] == RTCP_APP) && (memcmp(p + 8, name, 4) == 0)) {
  285.         *app_ptr = p;
  286.         return TRUE;
  287.     }
  288.     /* Advance to next subpacket */
  289.     p += (ntohs(*((short *) (p + 2))) + 1) * 4;
  290.     } while (p < end && (((p[0] >> 6) & 3) == RTP_VERSION));
  291.  
  292.     return FALSE;
  293. }
  294.  
  295. /*  RTP_MAKE_SDES  --  Generate a source description for this
  296.                user, based either on information obtained
  297.                from the password file or supplied by
  298.                environment variables.  Strict construction
  299.                of the RTP specification requires every
  300.                SDES packet to be a composite which begins
  301.                with a sender or receiver report.  If the
  302.                        "strict" argument is true, we'll comply with
  303.                        this.  Unfortunately, Look Who's Listening
  304.                Server code was not aware of this little
  305.                twist when originally implemented, so it will
  306.                take some time to transition all the running
  307.                servers to composite packet aware code.    */
  308.  
  309. int rtp_make_sdes(pkt, ssrc_i, port, strict)
  310.   char **pkt;
  311.   unsigned long ssrc_i;
  312.   int port, strict;
  313. {
  314.     unsigned char zp[1500];
  315.     unsigned char *p = zp;
  316.     rtcp_t *rp;
  317.     unsigned char *ap;
  318.     char *sp, *ep;
  319.     int l, hl;
  320.     struct passwd *pw;
  321.     char s[256], ev[1024];
  322.  
  323. #define addSDES(item, text) *ap++ = item; *ap++ = l = strlen(text); \
  324.                 bcopy(text, ap, l); ap += l
  325.  
  326.     hl = 0;
  327.     if (strict) {
  328.     *p++ = RTP_VERSION << 6;
  329.     *p++ = RTCP_RR;
  330.     *p++ = 0;
  331.     *p++ = 1;
  332.     *((long *) p) = htonl(ssrc_i);
  333.     p += 4;
  334.     hl = 8;
  335.     }
  336.  
  337.     rp = (rtcp_t *) p;
  338. #ifdef RationalWorld
  339.     rp->common.version = RTP_VERSION;
  340.     rp->common.p = 0;
  341.     rp->common.count = 1;
  342.     rp->common.pt = RTCP_SDES;
  343. #else
  344.     *((short *) p) = htons((RTP_VERSION << 14) | RTCP_SDES | (1 << 8));
  345. #endif    
  346.     rp->r.sdes.src = htonl(ssrc_i);
  347.  
  348.     ap = (unsigned char *) rp->r.sdes.item;
  349.  
  350.     ep = getenv("SPEAKFREE_ID");
  351.     if (ep != NULL) {
  352.     if (strlen(ep) == 0) {
  353.         ep = NULL;
  354.     } else {
  355.         strcpy(ev, ep);
  356.         ep = ev;
  357.     }
  358.     }
  359.  
  360.     /* Build canonical name for this user.  This is generally
  361.        a name which can be used for "talk" and "finger", and
  362.        is not necessarily the user's E-mail address. */
  363.  
  364.     if ((sp = getenv("SPEAKFREE_CNAME")) != NULL && strlen(sp) > 0) {
  365.         /* If strict, drop leading asterisk that's used to request an
  366.        unlisted entry on an LWL server. */
  367.         if (strict && sp[0] == '*') {
  368.         sp++;
  369.     }
  370.     addSDES(RTCP_SDES_CNAME, sp);
  371.     } else {
  372.     pw = getpwuid(getuid());
  373.     if (pw != NULL) {
  374.         char dn[64];
  375.         char hn[MAXHOSTNAMELEN];
  376.  
  377.         dn[0] = hn[0] = 0;
  378.         getdomainname(dn, sizeof dn);
  379.         gethostname(hn, sizeof hn);
  380.         if (dn[0] != 0) {
  381.                 sprintf(s, "%s@%s", pw->pw_name, dn);
  382.         } else {
  383.         struct hostent *h;
  384.         struct in_addr naddr;
  385.  
  386.         h = gethostbyname(hn);
  387.                 if (strchr(h->h_name, '.') != NULL) {
  388.                     sprintf(s, "%s@%s", pw->pw_name, h->h_name);
  389.         } else {
  390.             bcopy(h->h_addr, &naddr, sizeof naddr);
  391.                     sprintf(s, "%s@%s", pw->pw_name, inet_ntoa(naddr));
  392.         }
  393.         }
  394.         addSDES(RTCP_SDES_CNAME, s);
  395.         if (ep == NULL && pw->pw_gecos != NULL) {
  396.                 char *gc = strchr(pw->pw_gecos, ',');
  397.  
  398.         if (gc != NULL) {
  399.             *gc = 0;
  400.         }
  401.         addSDES(RTCP_SDES_NAME, pw->pw_gecos);
  402.         }
  403.     } else {
  404. #ifdef Solaris
  405.         {    char s[12];
  406.         sysinfo(SI_HW_SERIAL, s, 12);
  407.                 sprintf(s, "Unknown@%s.hostid.net", s);
  408.         }
  409. #else
  410.             sprintf(s, "Unknown@%lu.hostid.net", gethostid());
  411. #endif
  412.         addSDES(RTCP_SDES_CNAME, s);
  413.     }
  414.     }
  415.  
  416.     /* If a SPEAKFREE_ID environment variable is present,
  417.        parse the items it contains.  Format:
  418.  
  419.        SPEAKFREE_ID=<full name>:<E-mail address>:<phone number>:<location>
  420.  
  421.     */
  422.  
  423.     if (ep != NULL) {
  424.     int i;
  425.     static int items[] = { RTCP_SDES_NAME, RTCP_SDES_EMAIL,
  426.                    RTCP_SDES_PHONE, RTCP_SDES_LOC };
  427.     char *np;
  428.  
  429.     for (i = 0; i < ELEMENTS(items); i++) {
  430.         while (*ep && isspace(*ep)) {
  431.         ep++;
  432.         }
  433.         if (*ep == 0) {
  434.         break;
  435.         }
  436.             if ((np = strchr(ep, ':')) != NULL) {
  437.         *np++ = 0;
  438.         } else {
  439.         np = NULL;
  440.         }
  441.         if (strlen(ep) > 0) {
  442.                 /* If strict, drop leading asterisk that's used to request an
  443.            unlisted entry on an LWL server. */
  444.                 if (strict && items[i] == RTCP_SDES_EMAIL && ep[0] == '*') {
  445.             ep++;
  446.         }
  447.         addSDES(items[i], ep);
  448.         }
  449.         if (np == NULL) {
  450.         break;
  451.         }
  452.         ep = np;
  453.     }
  454.     }
  455.  
  456.     addSDES(RTCP_SDES_TOOL, "Speak Freely for Unix");
  457.  
  458.     if (!strict) {
  459.  
  460.     /* If a port number is specified, add a PRIV item indicating
  461.            the port we're communicating on. */
  462.  
  463.     if (port >= 0) {
  464.         char s[20];
  465.  
  466.             sprintf(s, "\001P%d", port);
  467.         addSDES(RTCP_SDES_PRIV, s);
  468.     }
  469.     }
  470.  
  471.     *ap++ = RTCP_SDES_END;
  472.     *ap++ = 0;
  473.  
  474.     l = ap - p;
  475.  
  476.     rp->common.length = htons(((l + 3) / 4) - 1);
  477.     l = hl + ((ntohs(rp->common.length) + 1) * 4);
  478.  
  479.     /* Okay, if the total length of this packet is not an odd
  480.        multiple of 4 bytes, we're going to put a pad at the
  481.        end of it.  Why?  Because we may encrypt the packet
  482.        later and that requires it be a multiple of 8 bytes,
  483.        and we don't want the encryption code to have to
  484.        know all about our weird composite packet structure.
  485.        Oh yes, there's no reason to do this if strict isn't
  486.        set, since we never encrypt packets sent to a Look
  487.        Who's Listening server.
  488.  
  489.        Why an odd multiple of 4 bytes, I head you ask?
  490.        Because when we encrypt an RTCP packet, we're required
  491.        to prefix it with four random bytes to deter a known
  492.        plaintext attack, and since the total buffer we
  493.        encrypt, including the random bytes, has to be a
  494.        multiple of 8 bytes, the message needs to be an odd
  495.        multiple of 4. */
  496.  
  497.     if (strict) {
  498.     int pl = (l & 4) ? l : l + 4;
  499.  
  500.     if (pl > l) {
  501.         int pad = pl - l;
  502.  
  503.         bzero(zp + l, pad);       /* Clear pad area to zero */
  504.         zp[pl - 1] = pad;          /* Put pad byte count at end of packet */
  505.             p[0] |= 0x20;             /* Set the "P" bit in the header of the
  506.                      SDES (last in message) packet */
  507.                                       /* If we've added an additional word to
  508.                      the packet, adjust the length in the
  509.                      SDES message, which must include the
  510.                      pad */
  511.         rp->common.length = htons(ntohs(rp->common.length) + ((pad) / 4));
  512.         l = pl;              /* Include pad in length of packet */
  513.     }
  514.     }
  515.  
  516.     *pkt = (char *) malloc(l);
  517.     if (*pkt != NULL) {
  518.     bcopy(zp, *pkt, l);
  519.     return l;
  520.     }
  521.     return 0;
  522. }
  523.  
  524. /*  RTP_MAKE_BYE  --  Create a "BYE" RTCP packet for this connection.  */
  525.  
  526. int rtp_make_bye(p, ssrc_i, raison, strict)
  527.   unsigned char *p;
  528.   unsigned long ssrc_i;
  529.   char *raison;
  530.   int strict;
  531. {
  532.     rtcp_t *rp;
  533.     unsigned char *ap, *zp;
  534.     int l, hl;
  535.  
  536.     /* If requested, prefix the packet with a null receiver
  537.        report.    This is required by the RTP spec, but is not
  538.        required in packets sent only to the Look Who's Listening
  539.        server. */
  540.  
  541.     zp = p;
  542.     hl = 0;
  543.     if (strict) {
  544.     *p++ = RTP_VERSION << 6;
  545.     *p++ = RTCP_RR;
  546.     *p++ = 0;
  547.     *p++ = 1;
  548.     *((long *) p) = htonl(ssrc_i);
  549.     p += 4;
  550.     hl = 8;
  551.     }
  552.  
  553.     rp = (rtcp_t *) p;
  554. #ifdef RationalWorld
  555.     rp->common.version = RTP_VERSION;
  556.     rp->common.p = 0;
  557.     rp->common.count = 1;
  558.     rp->common.pt = RTCP_BYE;
  559. #else
  560.     *((short *) p) = htons((RTP_VERSION << 14) | RTCP_BYE | (1 << 8));
  561. #endif    
  562.     rp->r.bye.src[0] = htonl(ssrc_i);
  563.  
  564.     ap = (unsigned char *) rp->r.sdes.item;
  565.  
  566.     l = 0;
  567.     if (raison != NULL) {
  568.     l = strlen(raison);
  569.     if (l > 0) {
  570.         *ap++ = l;
  571.         bcopy(raison, ap, l);
  572.         ap += l;
  573.     }
  574.     }
  575.  
  576.     while ((ap - p) & 3) {
  577.     *ap++ = 0;
  578.     }
  579.     l = ap - p;
  580.  
  581.     rp->common.length = htons((l / 4) - 1);
  582.  
  583.     l = hl + ((ntohs(rp->common.length) + 1) * 4);
  584.  
  585.     /* If strict, pad the composite packet to an odd multiple of 4
  586.        bytes so that if we decide to encrypt it we don't have to worry
  587.        about padding at that point. */
  588.  
  589.     if (strict) {
  590.     int pl = (l & 4) ? l : l + 4;
  591.  
  592.     if (pl > l) {
  593.         int pad = pl - l;
  594.  
  595.         bzero(zp + l, pad);       /* Clear pad area to zero */
  596.         zp[pl - 1] = pad;          /* Put pad byte count at end of packet */
  597.             p[0] |= 0x20;             /* Set the "P" bit in the header of the
  598.                      SDES (last in message) packet */
  599.                                       /* If we've added an additional word to
  600.                      the packet, adjust the length in the
  601.                      SDES message, which must include the
  602.                      pad */
  603.         rp->common.length = htons(ntohs(rp->common.length) + ((pad) / 4));
  604.         l = pl;              /* Include pad in length of packet */
  605.     }
  606.     }
  607.  
  608.     return l;
  609. }
  610.  
  611. /*  RTP_MAKE_APP  --  Create a "APP" (application-defined) RTCP packet
  612.               for this connection with the given type (name)
  613.               and content.  */
  614.  
  615. int rtp_make_app(p, ssrc_i, strict, type, content)
  616.   unsigned char *p;
  617.   unsigned long ssrc_i;
  618.   int strict;
  619.   char *type, *content;
  620. {
  621.     rtcp_t *rp;
  622.     unsigned char *ap, *zp;
  623.     int l, hl;
  624.  
  625.     /* If requested, prefix the packet with a null receiver
  626.        report.    This is required by the RTP spec, but is not
  627.        required in packets sent only to other copies of Speak
  628.        Freely. */
  629.  
  630.     zp = p;
  631.     hl = 0;
  632.     if (strict) {
  633.     *p++ = RTP_VERSION << 6;
  634.     *p++ = RTCP_RR;
  635.     *p++ = 0;
  636.     *p++ = 1;
  637.     *((long *) p) = htonl(ssrc_i);
  638.     p += 4;
  639.     hl = 8;
  640.     }
  641.  
  642.     rp = (rtcp_t *) p;
  643.     *((short *) p) = htons((RTP_VERSION << 14) | RTCP_APP | (1 << 8));
  644.     rp->r.bye.src[0] = htonl(ssrc_i);
  645.  
  646.     ap = p + 8;
  647.     bcopy(type, ap, 4);
  648.     ap += 4;
  649.  
  650.     l = strlen(content);
  651.     strcpy((char *) ap, content);
  652.     ap += l + 1;
  653.  
  654.     while ((ap - p) & 3) {
  655.     *ap++ = 0;
  656.     }
  657.     l = ap - p;
  658.  
  659.     rp->common.length = htons((l / 4) - 1);
  660.  
  661.     l = hl + ((ntohs(rp->common.length) + 1) * 4);
  662.  
  663.     /* If strict, pad the composite packet to an odd multiple of 4
  664.        bytes so that if we decide to encrypt it we don't have to worry
  665.        about padding at that point. */
  666.  
  667.     if (strict) {
  668.     int pl = (l & 4) ? l : l + 4;
  669.  
  670.     if (pl > l) {
  671.         int pad = pl - l;
  672.  
  673.         bzero(zp + l, pad);       /* Clear pad area to zero */
  674.         zp[pl - 1] = pad;          /* Put pad byte count at end of packet */
  675.             p[0] |= 0x20;             /* Set the "P" bit in the header of the
  676.                      SDES (last in message) packet */
  677.                                       /* If we've added an additional word to
  678.                      the packet, adjust the length in the
  679.                      SDES message, which must include the
  680.                      pad */
  681.         rp->common.length = htons(ntohs(rp->common.length) + ((pad) / 4));
  682.         l = pl;              /* Include pad in length of packet */
  683.     }
  684.     }
  685.  
  686.     return l;
  687. }
  688.  
  689. /*  RTPOUT  --    Convert a sound buffer into an RTP packet, given the
  690.         SSRC, timestamp, and sequence number appropriate for the
  691.         next packet sent to this connection.  */
  692.  
  693. int rtpout(sb, ssrc_i, timestamp_i, seq_i, spurt)
  694.   soundbuf *sb;
  695.   unsigned long ssrc_i, timestamp_i;
  696.   unsigned short seq_i;
  697.   int spurt;
  698. {
  699.     soundbuf rp;
  700.     rtp_hdr_t *rh = (rtp_hdr_t *) &rp;
  701.     int pl = 0;
  702.  
  703. #ifdef RationalWorld
  704.     rh->version = RTP_VERSION;
  705.     rh->p = 0;
  706.     rh->x = 0;
  707.     rh->cc = 0;
  708.     rh->m = !!spurt;
  709. #else
  710.     *((short *) rh) = htons((RTP_VERSION << 14) | (spurt ? 0x80 : 0));
  711. #endif
  712.     rh->seq = htons(seq_i);
  713.     rh->ts = htonl(timestamp_i);
  714.     rh->ssrc = htonl(ssrc_i);
  715.  
  716.     /* GSM */
  717.  
  718.     if (sb->compression & fCompGSM) {
  719. #ifdef RationalWorld
  720.     rh->pt = 3;
  721. #else
  722.     ((char *) rh)[1] = 3;
  723. #endif
  724.     bcopy(sb->buffer.buffer_val + 2, ((char *) &rp) + 12,
  725.           (int) sb->buffer.buffer_len - 2);
  726.     pl = (sb->buffer.buffer_len - 2) + 12;
  727.  
  728.     /* ADPCM */
  729.  
  730.     } else if (sb->compression & fCompADPCM) {
  731. #ifdef RationalWorld
  732.     rh->pt = 5;
  733. #else
  734.     ((char *) rh)[1] = 5;
  735. #endif
  736.     bcopy(sb->buffer.buffer_val, ((char *) &rp) + 12 + 4,
  737.           (int) sb->buffer.buffer_len - 3);
  738.     bcopy(sb->buffer.buffer_val + ((int) sb->buffer.buffer_len - 3),
  739.           ((char *) &rp) + 12, 3);
  740.     ((char *) &rp)[15] = 0;
  741.     pl = (sb->buffer.buffer_len + 1) + 12;
  742.  
  743.     /* LPC */
  744.  
  745.     } else if (sb->compression & fCompLPC) {
  746.     int i, n = (sb->buffer.buffer_len - 2) / 14;
  747.     char *ip = (char *) (sb->buffer.buffer_val + 2),
  748.          *op = (char *) &rp + 12;
  749. #ifdef RationalWorld
  750.     rh->pt = 7;
  751. #else
  752.     ((char *) rh)[1] = 7;
  753. #endif
  754.     for (i = 0; i < n; i++) {
  755.         bcopy(ip, op, 3);
  756.         bcopy(ip + 4, op + 3, 10);
  757.         op[13] = 0;
  758.         ip += 14;
  759.         op += 14;
  760.     }
  761.     pl = 12 + 14 * n;
  762.  
  763.     /* PCMU Uncompressed */
  764.  
  765.     } else {    /* Uncompressed PCMU samples */
  766. #ifdef RationalWorld
  767.     rh->pt = 0;
  768. #else
  769.     /* Already zeroed above */
  770. #endif
  771.     bcopy(sb->buffer.buffer_val, ((char *) &rp) + 12,
  772.         (int) sb->buffer.buffer_len);
  773.     pl = (int) sb->buffer.buffer_len + 12;
  774.     }
  775.     if (pl > 0) {
  776.     bcopy((char *) &rp, (char *) sb, pl);
  777.     }
  778.     return pl;
  779. }
  780.  
  781. /*  PARSESDES  --  Look for an SDES message in a possibly composite
  782.            RTCP packet and extract pointers to selected items
  783.                    into the caller's structure.  */
  784.  
  785. int parseSDES(packet, r)
  786.   unsigned char *packet;
  787.   struct rtcp_sdes_request *r;
  788. {
  789.     int i, success = FALSE;
  790.     unsigned char *p = packet;
  791.  
  792.     /* Initialise all the results in the request packet to NULL. */
  793.  
  794.     for (i = 0; i < r->nitems; i++) {
  795.     r->item[i].r_text = NULL;
  796.     }
  797.  
  798.     /* Walk through the individual items in a possibly composite
  799.        packet until we locate an SDES. This allows us to accept
  800.        packets that comply with the RTP standard that all RTCP packets
  801.        begin with an SR or RR. */
  802.  
  803.     while ((p[0] >> 6 & 3) == RTP_VERSION || (p[0] >> 6 & 3) == 1) {
  804.     if ((p[1] == RTCP_SDES) && ((p[0] & 0x1F) > 0)) {
  805.         unsigned char *cp = p + 8,
  806.               *lp = cp + (ntohs(*((short *) (p + 2))) + 1) * 4;
  807.  
  808.         bcopy(p + 4, r->ssrc, 4);
  809.         while (cp < lp) {
  810.         unsigned char itype = *cp;
  811.  
  812.         if (itype == RTCP_SDES_END) {
  813.             break;
  814.         }
  815.  
  816.         /* Search for a match in the request and fill the
  817.            first unused matching item.    We do it this way to
  818.            permit retrieval of multiple PRIV items in the same
  819.            packet. */
  820.  
  821.         for (i = 0; i < r->nitems; i++) {
  822.             if (r->item[i].r_item == itype &&
  823.             r->item[i].r_text == NULL) {
  824.             r->item[i].r_text = (char *) cp;
  825.             success = TRUE;
  826.             break;
  827.             }
  828.         }
  829.         cp += cp[1] + 2;
  830.         }
  831.         break;
  832.     }
  833.     /* If not of interest to us, skip to next subpacket. */
  834.     p += (ntohs(*((short *) (p + 2))) + 1) * 4;
  835.     }
  836.     return success;
  837. }
  838.  
  839. /*  COPYSDESITEM  --  Copy an SDES item to a zero-terminated user
  840.               string.  */
  841.  
  842. void copySDESitem(s, d)
  843.   char *s, *d;
  844. {
  845.     int len = s[1] & 0xFF;
  846.  
  847.     bcopy(s + 2, d, len);
  848.     d[len] = 0;
  849. }
  850.